/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lt.cj.core.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.lt.cj.core.filters.FilterConstans;


/*
 * @author luohong
 */
public class HttpAccesser {

    private static DefaultHttpClient httpclient;
    private static HttpAccesser accesser;

    private HttpAccesser() {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, Integer.MAX_VALUE);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
        ThreadSafeClientConnManager connMrg = new ThreadSafeClientConnManager(schemeRegistry);
        connMrg.setMaxTotal(120);                             // Increase max total connection to 200
        connMrg.setDefaultMaxPerRoute(120);                   // Increase default max connection per route to 20
        HttpHost localhost = new HttpHost("locahost", 100);   // Increase max connections for localhost:80 to 50
        connMrg.setMaxForRoute(new HttpRoute(localhost), 100);
        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("name", "boling");
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
        httpclient = new DefaultHttpClient(connMrg);
        httpclient.setCookieStore(cookieStore);
    }

    public static HttpAccesser getInstance() {

        if (accesser == null) {
            accesser = new HttpAccesser();
        }
        return accesser;
    }

    public String accessWebPage(String url) {

        String s = "";
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            s = EntityUtils.toString(entity);
        } catch (IOException ex) {
            Logger.getLogger(HttpAccesser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return s;
    }

    public String downloadPhoto(String phofilePath, String url) {

        String filePath = "";
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            filePath = phofilePath + "/" + new Date().getTime();
            int lastIndex = url.lastIndexOf(".");
            String suffix = url.substring(lastIndex);
            File photoFile = new File(filePath + suffix);
            if (photoFile.createNewFile()) {
                FileOutputStream fileOutputStream = new FileOutputStream(photoFile);
                fileOutputStream.write(EntityUtils.toByteArray(entity));
                fileOutputStream.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(HttpAccesser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
        } finally {
        }
        return filePath;
    }
}
